package io.shallwe.scanner.auto;

import org.tio.client.intf.ClientAioListener;
import org.tio.core.ChannelContext;
import org.tio.core.intf.Packet;

import com.jfinal.kit.LogKit;

import HslCommunication.Utilities;
import io.shallwe.scanner.manual.MessagePacket;

public class AutoScannerClientListener implements ClientAioListener{
	@Override
	public void onAfterConnected(ChannelContext context, boolean isConnected, boolean isReconnect) throws Exception {
		String ip = context.getServerNode().getIp();
		if(isConnected) {
			switch(ip) {
				case "172.30.31.57":
					LogKit.warn("\r\n连接上线自动扫描枪成功");
					break;
				case "172.30.31.58":
					LogKit.warn("\r\n连接下线自动扫描枪成功");
					break;
				default:
					break;
			}
			
		}else {
			switch(ip) {
			case "172.30.31.52":
				LogKit.warn("\r\n连接刻上线自动扫描枪失败,稍后会再次尝试.....");
				break;
			case "172.30.31.53":
				LogKit.warn("\r\n连接刻下线自动扫描枪失败,稍后会再次尝试.....");
				break;
			default:
				break;
		}
			
		}
	}

	@Override
	public void onAfterDecoded(ChannelContext context, Packet packet, int packetSize) throws Exception {
		
	}

	@Override
	public void onAfterHandled(ChannelContext context, Packet packet, long cost) throws Exception {
		
	}

	@Override
	public void onAfterReceivedBytes(ChannelContext context, int receivedBytes) throws Exception {
		
	}

	@Override
	public void onAfterSent(ChannelContext context, Packet packet,  boolean isSentSuccess) throws Exception {
		
		if(isSentSuccess){
			String ip = context.getServerNode().getIp();
			if(packet instanceof MessagePacket){
				MessagePacket messagePacket=(MessagePacket)packet;
				switch(ip) {
					case "172.30.31.52":
						LogKit.warn(String.format("向上线自动扫描枪发送读取条码命令:%s成功", 
								Utilities.bytes2HexString(messagePacket.getBody())));
						break;
					case "172.30.31.53":
						LogKit.warn(String.format("向下线自动扫描枪发送读取条码命令:%s成功", 
								Utilities.bytes2HexString(messagePacket.getBody())));
						break;
					default:
						break;
				}
			}
		}
	}

	@Override
	public void onBeforeClose(ChannelContext context, Throwable packet, String remark, boolean isRemove) throws Exception {
		String ip = context.getServerNode().getIp();
		switch(ip) {
			case "172.30.31.52":
				LogKit.warn("\r\n与上线自动扫描枪连接断开");
				break;
			case "172.30.31.53":
				LogKit.warn("\r\n与下线自动扫描枪连接断开");
				break;
			default:
				break;
		}
	}
}
